<?php

include_once "adminheader.inc";

$cleanPost = cleanInput($_POST);
$submit = $cleanPost['submit'];
if (isset($cleanPost['conf_id'])) {
	$conf_id = intval($cleanPost['conf_id']);
}

$conf_id = intval($conf_id);
// conference data
$cHdl =& $xoopsDB->query("SELECT *" .
						 " FROM " . $xoopsDB->prefix("confreg_conferences") .
						 ' WHERE conf_id = ' . $conf_id );
$cData = $xoopsDB->fetchArray($cHdl);

// FIX THIS!!!!
$date = 'August 26th, 2009';

$msg = "Thank you for attending the " . $cData['title'] . " meeting.\r\n";
$msg .= "Attached is a PDF of your Professional Development Certificate. If you find\r\n";
$msg .= "an error or have questions please e-mail Kirk Reh, TMCMCMC Chair, at <mailto:chair@tmcmcmc.org>.";

if (isset($conf_id) && ($submit != 'submit'))  {
 	echo 'Sending PDH Certs to all registrees.<p>';
// 262 records total
// 5 records starting at row 0 processed at Mon Nov 30 10:49:29 EST 2009
// 5 records starting at row 5 processed at Mon Nov 30 10:50:28 EST 2009
// 5 records starting at row 10 processed at Mon Nov 30 10:51:24 EST 2009
// 5 records starting at row 15 processed at Mon Nov 30 10:52:21 EST 2009
// 5 records starting at row 20 processed at Mon Nov 30 10:53:42 EST 2009
// 5 records starting at row 25 processed at Mon Nov 30 10:55:34 EST 2009
// 5 records starting at row 30 processed at Mon Nov 30 10:56:36 EST 2009
// 5 records starting at row 35 processed at Mon Nov 30 10:57:49 EST 2009
// 5 records starting at row 40 processed at Mon Nov 30 10:59:12 EST 2009
// 5 records starting at row 45 processed at Mon Nov 30 11:01:59 EST 2009
// 5 records starting at row 50 processed at Mon Nov 30 11:03:00 EST 2009
// 5 records starting at row 55 processed at Mon Nov 30 11:11:15 EST 2009
// 5 records starting at row 60 processed at Mon Nov 30 11:13:00 EST 2009
// 5 records starting at row 65 processed at Mon Nov 30 11:14:00 EST 2009
// 5 records starting at row 70 processed at Mon Nov 30 11:16:32 EST 2009
// 5 records starting at row 75 processed at Mon Nov 30 11:47:09 EST 2009
// 5 records starting at row 80 processed at Mon Nov 30 11:48:29 EST 2009
// 5 records starting at row 85 processed at Mon Nov 30 11:49:43 EST 2009
// 5 records starting at row 90 processed at Mon Nov 30 11:50:49 EST 2009
// 5 records starting at row 95 processed at Mon Nov 30 11:53:25 EST 2009
// 5 records starting at row 100 processed at Mon Nov 30 11:54:31 EST 2009
// 5 records starting at row 105 processed at Mon Nov 30 11:55:39 EST 2009
// 5 records starting at row 110 processed at Mon Nov 30 11:56:39 EST 2009
// 5 records starting at row 115 processed at Mon Nov 30 12:03:05 EST 2009
// 5 records starting at row 120 processed at Mon Nov 30 12:08:20 EST 2009
// 5 records starting at row 125 processed at Mon Nov 30 12:09:31 EST 2009
// 5 records starting at row 130 processed at Mon Nov 30 12:48:25 EST 2009
// 5 records starting at row 135 processed at Mon Nov 30 12:49:39 EST 2009
// 5 records starting at row 140 processed at Mon Nov 30 12:50:56 EST 2009
// -- 5 records starting at row 145 processed at Mon Nov 30 12:52:29 EST 2009









 	$start = (5 * 29);
 	$count = 5;
	$users = get_x_users($conf_id,$start,$count);
	foreach ($users as $uRec) {
		$name = $uRec['first_name'] . ' ' . $uRec['last_name'];
		if (strpos($uRec['email'],'@') === false) {
			echo 'skipping ' . $name . ' - invalid email address<p>';
			continue;
		}
		if (stripos($uRec['email'],'bentley') != false) {
			echo 'skipping ' . $name . ' - Bentley person<p>';
			continue;
		}

		if (substr_count($uRec['days_attending'],';') == 1) {
			$base_image = 'images/6_pdh_cert.png';
			$ctype = "6 hour";
		} else {
			$base_image = 'images/12_pdh_cert.png';
			$ctype = "12 hour";
		}

		echo 'Creating a ' . $ctype . ' pdh for ' . $name . '<br />';
		$file = XOOPS_CACHE_PATH . '/tmcmcmc_pdh_certificate.pdf';
		create_pdh_cert('',$name,$date,$base_image,$file);

		echo 'E-mailing pdf to ' . $uRec['email'] . '<br />';
		email_file('registration@tmcmcmc.org',$uRec['email'],'','TMCMCMC Professional Development certificate',$msg,$file,'application/pdf');

		echo 'Deleting temp files.<p>';
		unlink($file);
	}
	echo '// ' . $count . ' records starting at row ' . $start . ' processed at ' . date("D M j G:i:s T Y");
 	
} elseif (isset($conf_id) && ($submit == 'submit')) {
	$date = $cleanPost['date'];
	$cert_type = $cleanPost['cert_type'];
	if ($cert_type == 6) {
		$base_image = 'images/6_pdh_cert.png';
	} else {
		$base_image = 'images/12_pdh_cert.png';
	}

	$file = XOOPS_CACHE_PATH . '/tmcmcmc_pdh_certificate.pdf';
	echo 'Creating pdf...';
	create_pdh_cert('',$cleanPost['name'],$date,$base_image,$file);
	echo '<p>E-mailing pdf...';
	email_file('registration@tmcmcmc.org',$cleanPost['email'],'registration@tmcmcmc.org','tmcmcmc professional development certificate',$msg,$file,'application/pdf');
	echo '<p>Deleting temp files...';
	unlink($file);
	echo '<p>Done.<br />';
	echo '<form action="pdh_report.php" method=post>' . 
		 '<input type=hidden name=conf_id value=' . $conf_id . '>' .
		 '<input type=submit value="Click to return to PDH Reports"></form>';
} else {
	redirect_header(XOOPS_URL, 5, 'invalid conference id specified, redirecting to main page');
}

xoops_cp_footer();

/*
 * This file was generated by XBS ModGen, (c) 2006 A Kitson, UK. See http://xoobs.net
 * ModGen is a Module Code Generator for the Xoops CMS.  See http://xoops.org
 */
?>
